package com.mmall.controller.admin;

import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.CategoryService;
import com.mmall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value="/manager/category")
public class CategoryController {
    @Autowired
      private UserService userService;
    @Autowired
     private CategoryService categoryService;

 @RequestMapping("addCategory.do")
 @ResponseBody
    public ServerResponse addCategory(HttpSession session, @RequestParam("categoryname")String categoryname,
                                                           @RequestParam("parentId") Integer parentId){
     //校验用户是否处于登陆状态
        User  user = new User();
        user=(User) session.getAttribute("currentUser");
    if(user== null){
        return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(),"请登陆");
    }
    //校验用户身份
        if(userService.checkAdminRole(user).checkIsSuccess()){
          return   categoryService.addCategory(categoryname, parentId);
        }
        return ServerResponse.createServerResponseByFailure("权限不足");
    }

  //查询子节点的信息，并且不递归,查询平节点的
  @RequestMapping("getChildrenCategory.do")
  @ResponseBody
    public ServerResponse getChildrenCategory(HttpSession session, @RequestParam("parentId") Integer parentId){
        //校验用户是否处于登陆状态
        User  user = new User();
        user=(User) session.getAttribute("currentUser");
        if(user== null){
            return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(),"请登陆");
        }
        //校验用户身份
        if(userService.checkAdminRole(user).checkIsSuccess()){

               return categoryService.getChildrenCategory(parentId);
        }
        return ServerResponse.createServerResponseByFailure("权限不足");

    }
    @RequestMapping("getChildrenAllCategory.do")
    @ResponseBody
    //递归查询父节点下的所有商品信息
    public ServerResponse getChildrenAllCategory(HttpSession session, @RequestParam("parentId") Integer parentId){

        return  categoryService.getChildrenAllCategory(parentId);
    }
    @RequestMapping("updateCategoryname.do")
    @ResponseBody
    //递归查询父节点下的所有商品信息
    public ServerResponse updateCategoryname(HttpSession session, @RequestParam("categoryName") String categoryName,
                                                                   @RequestParam("categoryId") Integer categoryId){
        User  user = new User();
        user=(User) session.getAttribute("currentUser");
        if(user== null){
            return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(),"请登陆");
        }
        //校验用户身份
        if(userService.checkAdminRole(user).checkIsSuccess()){

            categoryService.updateCategoryname(categoryId,categoryName);

        }
        return ServerResponse.createServerResponseByFailure("权限不足");

    }
}
